home *** CD-ROM | disk | FTP | other *** search
- /*
- * FILE: menu.c
- * AUTHOR: R. Gonzalez
- * CREATED: August 25, 1990
- *
- * Defines menu methods, required for all types of application.
- */
-
- # include <string.h>
- # include <stdlib.h>
- # include "menu.h"
- # include "morestr.h"
-
- /************************************************************************
- * initialize menu
- ************************************************************************/
- boolean Menu::init(void)
- {
- int i;
-
- num_menus = 0;
-
- for (i=1 ; i<=MAX_MENUS ; i++)
- num_items[i] = 0;
-
- return TRUE;
- }
-
- /************************************************************************
- * add command to menu. Space must be allocated statically by calling
- * routine; e.g., argument may be a constant string like "Quit".
- * By convention the menu numbers start at 1 while the item numbers
- * start at 0. The 0th item in each menu is the menu's heading.
- * (This is ignored in command-line applications.) It may be assoc-
- * iated with the comfunc: NULL. Be careful not to duplicate menu
- * names (see the unimplemented function below), nor to exceed the
- * MAX_MENUS and MAX_ITEMS limits, nor to leave any "empty" items
- * whose item number is smaller than num_items[menu_num].
- ************************************************************************/
- boolean Menu::log_command(int menu_num,int item,char *command,
- comfunc command_ptr)
- {
- boolean success;
-
- if (menu_num <= MAX_MENUS && item <= MAX_ITEMS)
- {
- if (menu_num > num_menus)
- num_menus = menu_num;
- if (item+1 > num_items[menu_num])
- num_items[menu_num] = item + 1;
- this->command[menu_num][item] = command;
- this->command_ptr[menu_num][item] = command_ptr;
- success = TRUE;
- }
- else
- success = FALSE;
-
- return success;
- }
-
- # ifdef never
- /************************************************************************
- * check if command already exists - presently unused function
- ************************************************************************/
- boolean Menu::command_exists(char *command)
- {
- int menu_num,
- item;
- boolean exists = FALSE;
-
- for (menu_num=1 ; menu_num <= num_menus && !exists ; menu_num++)
- for (item=0 ; item < num_items[menu_num] && !exists ; item++)
- if (strsame(this->command[menu_num][item],command))
- exists = TRUE;
-
- return exists;
- }
- # endif
-
- /************************************************************************
- * get menu number
- ************************************************************************/
- int Menu::get_menu(char *command)
- {
- int menu_num,
- item;
- boolean done = FALSE;
-
- for (menu_num=1 ; menu_num <= num_menus && !done ; menu_num++)
- for (item=1 ; item < num_items[menu_num] && !done ; item++)
- if (strsame(this->command[menu_num][item],command))
- done = TRUE;
-
- if (done)
- return menu_num-1;
- else
- return ILLEGAL;
- }
-
- /************************************************************************
- * get item number
- ************************************************************************/
- int Menu::get_item(char *command)
- {
- int menu_num,
- item;
- boolean done = FALSE;
-
- for (menu_num=1 ; menu_num <= num_menus && !done ; menu_num++)
- for (item=1 ; item < num_items[menu_num] && !done ; item++)
- if (strsame(this->command[menu_num][item],command))
- done = TRUE;
-
- if (done)
- return item-1;
- else
- return ILLEGAL;
- }
-
- /************************************************************************
- * get pointer to command function associated with menu_num and item.
- ************************************************************************/
- comfunc Menu::get_command_ptr(int menu_num,int item)
- {
- if (menu_num < 1 || menu_num > num_menus)
- return NULL;
- else if (item < 1 || item >= num_items[menu_num])
- return NULL;
- else
- return command_ptr[menu_num][item];
- }
-
-